<template>
  <NCard title="定时任务管理" :bordered="false" size="small" class="mb-4">
    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
      <!-- 任务同步定时 -->
      <div class="flex flex-col space-y-2 p-4 border rounded-lg">
        <div class="flex items-center justify-between">
          <span class="text-sm font-medium">任务数据同步</span>
          <NTag type="info" size="small">每小时执行</NTag>
        </div>
        <div class="text-xs text-gray-500">
          Cron: 0 0 * * * ?
        </div>
        <div class="text-xs text-gray-500">
          下次执行: {{ getNextExecutionTime('0 0 * * * ?') }}
        </div>
        <NButton
          size="small"
          type="primary"
          ghost
          @click="handleManualSync('tasks')"
        >
          立即执行
        </NButton>
      </div>

      <!-- 通话记录同步定时 -->
      <div class="flex flex-col space-y-2 p-4 border rounded-lg">
        <div class="flex items-center justify-between">
          <span class="text-sm font-medium">通话记录同步</span>
          <NTag type="info" size="small">每30分钟执行</NTag>
        </div>
        <div class="text-xs text-gray-500">
          Cron: 0 */30 * * * ?
        </div>
        <div class="text-xs text-gray-500">
          下次执行: {{ getNextExecutionTime('0 */30 * * * ?') }}
        </div>
        <NButton
          size="small"
          type="primary"
          ghost
          @click="handleManualSync('callRecords')"
        >
          立即执行
        </NButton>
      </div>

      <!-- 剧本同步定时 -->
      <div class="flex flex-col space-y-2 p-4 border rounded-lg">
        <div class="flex items-center justify-between">
          <span class="text-sm font-medium">剧本数据同步</span>
          <NTag type="info" size="small">每天凌晨2点执行</NTag>
        </div>
        <div class="text-xs text-gray-500">
          Cron: 0 0 2 * * ?
        </div>
        <div class="text-xs text-gray-500">
          下次执行: {{ getNextExecutionTime('0 0 2 * * ?') }}
        </div>
        <NButton
          size="small"
          type="primary"
          ghost
          @click="handleManualSync('scripts')"
        >
          立即执行
        </NButton>
      </div>
    </div>

    <!-- 说明信息 -->
    <div class="mt-4 p-3 bg-blue-50 border border-blue-200 rounded-lg">
      <div class="text-sm text-blue-800">
        <div class="font-medium mb-2">定时任务说明：</div>
        <ul class="list-disc list-inside space-y-1 text-xs">
          <li>任务数据同步：每小时自动同步一次，确保任务状态最新</li>
          <li>通话记录同步：每30分钟同步一次，及时获取通话结果</li>
          <li>剧本数据同步：每天凌晨2点同步，避免影响业务高峰期</li>
          <li>所有定时任务都支持手动触发，可随时执行</li>
        </ul>
      </div>
    </div>
  </NCard>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { NCard, NButton, NTag } from 'naive-ui';
import { fetchManualSync } from '@/service/api/outbound';

// 计算下次执行时间（简化版本）
const getNextExecutionTime = (cron: string) => {
  const now = new Date();
  let nextTime = new Date(now);

  if (cron === '0 0 * * * ?') {
    // 每小时执行
    nextTime.setHours(now.getHours() + 1, 0, 0, 0);
  } else if (cron === '0 */30 * * * ?') {
    // 每30分钟执行
    const minutes = now.getMinutes();
    const nextMinute = Math.ceil(minutes / 30) * 30;
    if (nextMinute === 60) {
      nextTime.setHours(now.getHours() + 1, 0, 0, 0);
    } else {
      nextTime.setMinutes(nextMinute, 0, 0, 0);
    }
  } else if (cron === '0 0 2 * * ?') {
    // 每天凌晨2点执行
    nextTime.setDate(now.getDate() + 1);
    nextTime.setHours(2, 0, 0, 0);
  }

  return nextTime.toLocaleString('zh-CN');
};

// 手动执行同步
const handleManualSync = async (type: 'tasks' | 'callRecords' | 'scripts') => {
  try {
    await fetchManualSync(type);
    window.$message.success(`${type}同步已启动`);
  } catch (error) {
    window.$message.error(`${type}同步启动失败`);
    console.error('同步启动失败:', error);
  }
};
</script>
